<?php
/*
	Muffinz CMS - A website content management system.
	Copyright (C) 2010-2011 Sigitas Zelenkovas

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
	
	Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/

class User{
	private $userdata = array();
	private $userSettings = array();
	
	public function __construct(){
		@$this->userdata = $_SESSION['user'];
	}
	
	public function GetFlags(){
		return explode(" ", $this->userdata['flags']);
	}
	
	public function Flag($flag){
		foreach(explode(" ", $this->userdata['flags']) as $flg){
			if($flg == $flag){ return true; }
		}
		return false;
	}
	
	public function GetAvatar($img = false){
		if($img){
			if(!isset($_SESSION['user']['settings']['avatar'])){
				return "<img src=\"".$_SESSION['sys']['core_link_full'].$_SESSION['sys']['core_link'].$_SESSION['sys']['core_avatar_default']."\" />";
			}else {
				return "<img src=\"".$_SESSION['sys']['core_link_full'].$_SESSION['sys']['core_link'].$_SESSION['sys']['core_avatar_dir'].$_SESSION['user']['username']."/".$_SESSION['user']['settings']['avatar']."\" />";
			}
		}else {
			if(!isset($_SESSION['user']['settings']['avatar'])){
				return $_SESSION['sys']['core_link_full'].$_SESSION['sys']['core_link'].$_SESSION['sys']['core_avatar_default'];
			}else {
				return $_SESSION['sys']['core_link_full'].$_SESSION['sys']['core_link'].$_SESSION['sys']['core_avatar_dir'].$_SESSION['user']['username']."/".$_SESSION['user']['settings']['avatar'];
			}
		}
	}
	
	public function SetSetting($name, $value){ 
		if($this->userSettings == array()){
			$this->userSettings = json_decode($_SESSION['user']['rawsettings'], true);
		}
		$this->userSettings[$name] = $value;
	}
	
	public function SaveSettings(){
		global $q;
	
		$data = json_encode($this->userSettings);
		$q->Send("UPDATE users SET settings=\"".mysql_real_escape_string($data)."\" WHERE id={$_SESSION['user']['id']}");
		
		$data = $q->Send("SELECT * FROM users WHERE id={$_SESSION['user']['id']}");
		$this->ParseSettings($data);
	}
	
	public function ParseSettings($data){
		global $q;
		
		$_SESSION['sys'] = $q->GetOp(true);
		$_SESSION['user'] = $q->Fetch_array($data, MYSQL_ASSOC);
		
		$_SESSION['user']['rawsettings'] = $_SESSION['user']['settings'];
		$_SESSION['user']['rawsother'] = $_SESSION['user']['other'];
		
		$_SESSION['user']['settings'] = json_decode($_SESSION['user']['settings'], true);
		$_SESSION['user']['other'] = json_decode($_SESSION['user']['other'], true);
	}
}

$user = new User();

class Profile{
	private $data = array();
	
	function __construct($data, $name){
		$this->data = json_decode($data, true);
		$this->data['username'] = $name;
	}
	
	public function GetAvatar($img = false){
		if($img){
			if(!isset($this->data['avatar'])){
				return "<img src=\"".$_SESSION['sys']['core_link_full'].$_SESSION['sys']['core_link'].$_SESSION['sys']['core_avatar_default']."\" />";
			}else {
				return "<img src=\"".$_SESSION['sys']['core_link_full'].$_SESSION['sys']['core_link'].$_SESSION['sys']['core_avatar_dir'].$this->data['username']."/".$this->data['avatar']."\" />";
			}
		}else {
			if(!isset($this->data['avatar'])){
				return $_SESSION['sys']['core_link_full'].$_SESSION['sys']['core_link'].$_SESSION['sys']['core_avatar_default'];
			}else {
				return $_SESSION['sys']['core_link_full'].$_SESSION['sys']['core_link'].$_SESSION['sys']['core_avatar_dir'].$this->data['username']."/".$this->data['avatar'];
			}
		}
	}
}